package com.zmt.mrms.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.zmt.mrms.entity.MedicalRecord;
import com.zmt.mrms.entity.MedicineList;
import com.zmt.mrms.entity.PrescriptionMedicine;
import com.zmt.mrms.entity.PrescriptionRecord;
import com.zmt.mrms.entity.UserDoctor;
import com.zmt.mrms.mapper.DoctorMapper;
import com.zmt.mrms.utils.FormatterUtils;

import jakarta.annotation.Resource;

@Service
public class DoctorService {

	@Resource
	private DoctorMapper doctorMapper;

	FormatterUtils formatterUtils = new FormatterUtils();

	public String finishOnePatient(int medicalRecordId) {
		doctorMapper.finishPrescription(medicalRecordId, formatterUtils.nowDateTime());
		doctorMapper.finishMedical(medicalRecordId, formatterUtils.nowDateTime());
		return "";
	}

	public Map<String, List<PrescriptionMedicine>> getPrescriptionRecord() {
		List<PrescriptionRecord> list = doctorMapper.prepareForGetMedicine();
		Map<String, List<PrescriptionMedicine>> map = new HashMap<>();
		for (int i = 0, len = list.size(); i < len; i++) {
			int medicalId = list.get(i).getMedicalRecordId();
			Boolean ispaidBoolean = doctorMapper.isPaid(medicalId).isMedicineStatus();
			if (ispaidBoolean != true) {
				list.remove(i);
				len--;
				i--;
			} else {
				List<PrescriptionMedicine> list2 = JSONObject.parseArray(list.get(i).getPrescription(),
						PrescriptionMedicine.class);
				map.put(Integer.toString(list.get(i).getMedicalRecordId()), list2);
			}
		}

		return map;
	}

	public String finishPreRecord(int id, int doctorId) {
		PrescriptionRecord prescriptionRecord = new PrescriptionRecord();
		prescriptionRecord.setMedicalRecordId(id);
		prescriptionRecord.setApothecaryId(doctorId);
		prescriptionRecord.setDoubleCheck(true);
		prescriptionRecord.setFinishTime(formatterUtils.nowDateTime());
		doctorMapper.doubleCheck(prescriptionRecord);
		String medicineString = doctorMapper.getPreRecord(id).getPrescription();
		List<PrescriptionMedicine> medicineJson = JSONObject.parseArray(medicineString, PrescriptionMedicine.class);
		Double price = 0.00;
		for (int i = 0; i < medicineJson.size(); i++) {
//			System.out.println(medicineJson.get(i));
			String medicineName = medicineJson.get(i).getName();
			String amountString = medicineJson.get(i).getAmount();
			Double medicinePrice = doctorMapper.getMedicineInfo(medicineName).getPrice();
			price = price + medicinePrice * Integer.parseInt(amountString);
		}
		doctorMapper.setPaymentMedinicePrice(price, id);
		return "";
	}

	public List<PrescriptionRecord> getPreRecord() {
		return doctorMapper.getPrescriptionRecord();
	}

	public String setPrescription(PrescriptionRecord preRecord) {
		preRecord.setCreateTime(formatterUtils.nowDateTime());
//		System.out.println(preRecord);
		doctorMapper.setPrescription(preRecord);
		return "";
	}

	public MedicalRecord getMedicalRecord(int id) {
		if (doctorMapper.isPaid(id).isRegisteredStatus()) {
			return doctorMapper.getMedicalRecord(id);
		}
		return new MedicalRecord();
	}

	public List<MedicineList> getMedicineList() {
		return doctorMapper.getMedicineList();
	}

	/**
	 * 登录模块、个人信息模块
	 */

	public int updateDoctorInfo(UserDoctor userDoctor) {
		return doctorMapper.updateDoctorInfo(userDoctor);
	}

	public UserDoctor getDoctorInfo(int id) {
		return doctorMapper.findDoctorbyId(id);
	}

	public UserDoctor loginbyDoctor(String account, String pwd) {
		return doctorMapper.findDoctor(account, pwd);
	}

}
